﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace ReadSystemInfo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            
            InitializeComponent();
            
             richTextBox1.AppendText(ReadPCOperateTime());
        }
        public static string ReadPCOperateTime()
        {
            EventLog eventlog = new EventLog();
            //"Application"应用程序, "Security"安全, "System"系统  
            eventlog.Log = "System";
            EventLogEntryCollection eventLogEntryCollection = eventlog.Entries;
            StringBuilder sb = new StringBuilder();
            foreach (EventLogEntry entry in eventLogEntryCollection)
            {
                if (entry.InstanceId == 12)
                {
                    sb.AppendFormat("日期：{0},开机时间为：{1}"
                        , entry.TimeGenerated.ToLongDateString()
                        , entry.TimeGenerated.ToLongTimeString());
                    sb.AppendLine();
                }
                if (entry.InstanceId == 13)
                {
                    sb.AppendFormat("日期：{0},关机时间为：{1}"
                        , entry.TimeGenerated.ToLongDateString()
                        , entry.TimeGenerated.ToLongTimeString());
                    sb.AppendLine();
                }
            }
            return sb.ToString();
        }  
    }
}
